<?php
namespace app\index\controller;
use youdao\Api;
use think\Db;
use app\index\model\Content;
use app\index\model\Ts ;
ini_set('max_execution_time','50000');
class Animal extends \think\Controller{
    
    public function ts($text=''){

        

        
        $youdao = new Api();
       
    
           // var_dump($v);exit;
        $name=$this->request->post('name');
        $content=$this->request->post('content');
        $a_id=$this->request->post('a_id');
        $len=mb_strlen($content);
            if($len>5000){
                $text= mb_substr( $content, 0, 4990, 'utf-8' ) ;
            }
            else{
                $text= $content;
            }
		
            $ret=$youdao ->do_request($text,'en');
           
            $ret = json_decode($ret, true);
            if($ret['errorCode']>0){
                echo $ret['errorCode']."-".date('Y-m-d H:i:s')."\n";
                exit();
  
            }
            
            $en_content=$ret['translation'][0];
            $ret=$youdao ->do_request($name,'en');
            
            $ret = json_decode($ret, true);
            if($ret['errorCode']>0){
                echo $ret['errorCode']."-".date('Y-m-d H:i:s')."\n";
                exit();
                
            }

            $en_name=$ret['translation'][0];
            $res['en_name'] =$en_name;
            $res['en_content'] =$en_content;
            
     
            $where['a_id']=$a_id;

            $en_content_list= Db::name('ts_info_en')->where($where)->select();

            if(count($en_content_list)>0){

                $ts =new Ts();
                $ts->save([
                    'name'  => $en_name,
                    'content' => $en_content
                ],['a_id' => $a_id]);
            }
            else{
                $ts =new Ts();
                $ts->name = $en_name;
                $ts->content = $en_content;
                $ts->a_id = $a_id;
                $ts->save();
            }
        echo json_encode($res);
     // echo 'ok' ;
      exit;
    }
    

    function creatimage(){
        $a_id=$this->request->post('a_id');
        
        $sql="delete  from file_list where a_id=".$a_id." and type = 1 ";
        Db::execute($sql);
        $content=Content::get($a_id);
        $type=$content->type;
        $image_url_list=catch_img($content->name);
      
        $list=json_decode($image_url_list,true);
        
        $folder="/var/www/timo/data/temp_image/";
       
        foreach ($list as $k=>$v){
            $file_name=$a_id."_".$k.".jpg";
            $path=$folder.$file_name;
            savefile($v,$file_name);
            $oss_key=oss_upload_file($folder,$type, $file_name);
            $sql="insert into  file_list set bucket='timo-public', oss_key='".$oss_key."', a_id=".$a_id.",type = 1 ";

            Db::execute($sql);
            
            if (file_exists($path)) {
                  unlink($path);
            }
        }
        
        echo 'sucess';
        exit();
    }
    
    function creataudio(){
        $a_id=$this->request->post('a_id');
//         /$a_id=73668;
        $content=Content::get($a_id);
 
        $type=$content->type;
        $name=$content->name;
        $content=$content->content;
       
        
        $where['a_id']=$a_id;
        $en_content= Db::name('ts_info_en')->where($where)->find();
        
        $sql="delete  from file_list where a_id=".$a_id." and (type = 2 or type = 4) ";
        Db::execute($sql);
        
       
        $status1=tts_one($content,$type,$a_id,'zh',2); 
        $status2=tts_one($en_content['content'],$type,$a_id,'eng',2);
        
        $status3=tts_one($name,$type,$a_id,'zh',4); 
        $status4=tts_one($en_content['name'],$type,$a_id,'eng',4); 
        if($status1==1 && $status2==1 && $status3==1 && $status4==1){
            echo 'sucess';
            exit();
        }
        else{
            echo 'fail';
            exit();
        }
    }
    
    
    function createvideo(){
        
        
        
        $localpath="/var/www/timo/data/temp_video/";
        $a_id=$this->request->post('a_id');
        
        $sql="delete  from file_list where a_id=".$a_id." and type = 3 ";
        Db::execute($sql);
        
        $where['a_id']=$a_id;
        $content= Db::name('content')->where($where)->find();
        $type=$content['type'];
        
        $temp_path=$localpath.$a_id."/";
        if (!file_exists($temp_path)) {
            mkdir($temp_path, 0777);
            @chmod($temp_path, 0777);
        }
        $s1 = "select * from file_list where a_id='".$a_id."' AND type=1 limit 4";
        $image_list=Db::query($s1);
        $image_count=count($image_list);
        if($image_count>0){
            foreach ($image_list as $k1=>$v1){
                if($k1<4){
                    $localfile=$temp_path."image".$k1.".jpg";
                    ossdown($v1['oss_key'], $localfile,'timo-public');
                    suofang($localfile);
                }
            }
        }
        else{
            $sql="update  content set is_video=3 where a_id=".$a_id;
            Db::execute($sql);
            $sql="update  ts_info_en set is_video=3 where a_id=".$a_id;
            Db::execute($sql);
            echo $a_id."-没有图片视频生成失败";
            exit;
        }
        
        $s2 = "select * from file_list where a_id='".$a_id."' AND type=2 AND language='eng' limit 1";
        $audio_en=Db::query($s2);
        $audioc_en_count=count($audio_en);
        if($audioc_en_count>0){
            $audiofile_en=$temp_path."audio-en.mp3";
            ossdown($audio_en[0]['oss_key'], $audiofile_en,'timo-video');
        }
        else{
            $sql="update  content set is_video=5 where a_id=".$a_id;
            Db::execute($sql);
            $sql="update  ts_info_en set is_video=5 where a_id=".$a_id;
            Db::execute($sql);
            echo $a_id."-没有英文音频视频生成失败";
            exit;
        }
        
        $s3 = "select * from file_list where a_id='".$a_id."' AND type=2 AND language='zh' limit 1";
        $audio_cn=Db::query($s3);
        $audioc_cn_count=count($audio_cn);
        if($audioc_cn_count>0){
            $audiofile_cn=$temp_path."audio-cn.mp3";
            ossdown($audio_cn[0]['oss_key'], $audiofile_cn,'timo-video');
        }
        else{
            $sql="update  content set is_video=4 where a_id=".$a_id;
            Db::execute($sql);
            $sql="update  ts_info_en set is_video=4 where a_id=".$a_id;
            Db::execute($sql);
            $cmd ="rm -rf ".$temp_path;
            exec($cmd);
            echo $a_id."-没有中文音频视频生成失败";
           exit;
        }
        
        $config_status_en=creat_video_config_file($temp_path,'en' ,$image_count);
        $config_status_cn=creat_video_config_file($temp_path,'cn' ,$image_count);
        
        if($config_status_en&&$config_status_cn){
            $config_file_cn=$temp_path."config-cn.txt";
            $video_cn=$temp_path."video-cn.mp4";
            $config_file_en=$temp_path."config-en.txt";
            $video_en=$temp_path."video-en.mp4";
            $s1=creat_mp4_re($config_file_cn,$audiofile_cn,$temp_path,$type,"zh");
            
            $s2=creat_mp4_re($config_file_en,$audiofile_en,$temp_path,$type,"eng");
            
            if($s1 && $s2 ){
                $oss_cn_url=oss_upload_file($temp_path,$type,"video-cn-264.mp4",false,'timo-video');
                $oss_en_url=oss_upload_file($temp_path,$type,"video-en-264.mp4",false,'timo-video');
                $sql="insert into  file_list set bucket='timo-video', oss_key='".$oss_cn_url."', a_id=".$a_id.",type = 3,language = 'zh' ";
                Db::execute($sql);
                $sql="insert into  file_list set bucket='timo-video', oss_key='".$oss_en_url."', a_id=".$a_id.",type = 3,language = 'eng' ";
                Db::execute($sql);
                $sql="update  content set is_video=1 where a_id=".$a_id;
                Db::execute($sql);
                $sql="update  ts_info_en set is_video=1 where a_id=".$a_id;
                Db::execute($sql);
                $cmd ="rm -rf ".$temp_path;
                exec($cmd);
                
                echo "视频生成成功";
                exit;
            }
            else{
                $sql="update  content set is_video=2 where a_id=".$a_id;
                Db::execute($sql);
                $sql="update  ts_info_en set is_video=2 where a_id=".$a_id;
                Db::execute($sql);
                $cmd ="rm -rf ".$temp_path;
                exec($cmd);
                echo $a_id."-视频生成失败";
                exit;
            }
        }
        else{
            $sql="update  content set is_video=6 where a_id=".$a_id;
            Db::execute($sql);
            $sql="update  ts_info_en set is_video=6 where a_id=".$a_id;
            Db::execute($sql);
            $cmd ="rm -rf ".$temp_path;
            exec($cmd);
            echo $a_id."-音频异常导致配置文件出错视频生成失败";
            exit;
        }
        
    }
    
    function upmp4(){
        echo "开始时间:".date("Y-m-d h:i:s");
        echo "<br>";
        $sql ="select * from content where is_video =1 limit 50";
        $result = Db::query($sql);
        $localpath="/var/www/timo/data/temp_video/";
        foreach ($result as $k=>$v){
            $a_id=$v['a_id'];
            $type=$v['type'];
            $temp_path=$localpath.$a_id."/";
            if (!file_exists($temp_path)) {
                mkdir($temp_path, 0777);
                @chmod($temp_path, 0777);
            }
            $s1 = "select * from file_list where a_id='".$a_id."' AND type=3 ";
            $video_list=Db::query($s1);
            foreach ($video_list as $k1=>$v1){
                if($v1['language']=="zh"){
                    $localfile=$temp_path."video_zh.mp4";
                }
                else{
                    $localfile=$temp_path."video_eng.mp4";
                }
                
                ossdown($v1['oss_key'], $localfile,'timo-video');
            }
            $cmd1="/usr/bin/ffmpeg -threads 6 -i ".$temp_path."video_zh.mp4  -vf scale=640:480  -r 30 -vcodec h264  ".$temp_path."video_zh_264.mp4";
            $cmd2="/usr/bin/ffmpeg -threads 6 -i ".$temp_path."video_eng.mp4  -vf scale=640:480  -r 30 -vcodec h264 ".$temp_path."video_eng_264.mp4";
            exec($cmd1, $log, $status);
            if($status>0){
                continue;
            }
            exec($cmd2, $log, $status2);

            if($status2>0){
                continue;
            }
            $oss_cn_url=oss_upload_file($temp_path,$type,"video_zh_264.mp4",false,'timo-video');
            $oss_en_url=oss_upload_file($temp_path,$type,"video_eng_264.mp4",false,'timo-video');
          echo  $sql="update file_list set oss_key='".$oss_cn_url."' where a_id=".$a_id." and type = 3 and language = 'zh' ";
          echo "<br>";
            Db::execute($sql);
      echo      $sql="update file_list set oss_key='".$oss_en_url."' where a_id=".$a_id." and type = 3 and language = 'eng' ";
            Db::execute($sql);
            echo "<br>";
          echo  $sql="update content set is_video = '9' where a_id=".$a_id."  ";
            Db::execute($sql);
            echo "<br>";
            $cmd ="rm -rf ".$temp_path;
            exec($cmd);
        }
         echo "结束时间:".date("Y-m-d h:i:s");
         echo "<br>";
    }
    
    
    
    function rupmp4(){
        echo "开始时间:".date("Y-m-d h:i:s");
        echo "<br>";
        $sql ="select * from content where is_video =1 order by a_id desc limit 400";
        $result = Db::query($sql);
        $localpath="/var/www/timo/data/temp_video/";
        foreach ($result as $k=>$v){
            $a_id=$v['a_id'];
            $type=$v['type'];
            $temp_path=$localpath.$a_id."/";
            if (!file_exists($temp_path)) {
                mkdir($temp_path, 0777);
                @chmod($temp_path, 0777);
            }
            $s1 = "select * from file_list where a_id='".$a_id."' AND type=3 ";
            $video_list=Db::query($s1);
            foreach ($video_list as $k1=>$v1){
                if($v1['language']=="zh"){
                    $localfile=$temp_path."video_zh.mp4";
                }
                else{
                    $localfile=$temp_path."video_eng.mp4";
                }
                
                ossdown($v1['oss_key'], $localfile,'timo-video');
            }
            $cmd1="/usr/bin/ffmpeg -threads 5 -i ".$temp_path."video_zh.mp4  -vf scale=640:480  -r 30 -vcodec h264  ".$temp_path."video_zh_264.mp4";
            $cmd2="/usr/bin/ffmpeg -threads 5 -i ".$temp_path."video_eng.mp4  -vf scale=640:480  -r 30 -vcodec h264 ".$temp_path."video_eng_264.mp4";
            exec($cmd1, $log, $status);
            if($status>0){
                continue;
            }
            exec($cmd2, $log, $status);
            if($status>0){
                continue;
            }
            $oss_cn_url=oss_upload_file($temp_path,$type,"video_zh_264.mp4",false,'timo-video');
            $oss_en_url=oss_upload_file($temp_path,$type,"video_eng_264.mp4",false,'timo-video');
            $sql="update file_list set oss_key='".$oss_cn_url."' where a_id=".$a_id." and type = 3 and language = 'zh' ";
            Db::execute($sql);
            $sql="update file_list set oss_key='".$oss_en_url."' where a_id=".$a_id." and type = 3 and language = 'eng' ";
            Db::execute($sql);
            
            $sql="update content set is_video = '9' where a_id=".$a_id."  ";
            Db::execute($sql);
            $cmd ="rm -rf ".$temp_path;
            exec($cmd);
        }
        echo "结束时间:".date("Y-m-d h:i:s");
        echo "<br>";
    }
    
    function repairdata(){
        $sql ="select * from content where is_video =2 ";
        $result = Db::query($sql);
        $localpath="/var/www/timo/data/temp_video/";
        foreach ($result as $k=>$v){
            $a_id=$v['a_id'];
            $type=$v['type'];
            $temp_path=$localpath.$a_id."/";
            if (!file_exists($temp_path)) {
                mkdir($temp_path, 0777);
                @chmod($temp_path, 0777);
            }
            $s1 = "select * from file_list where a_id='".$a_id."' AND type=1 limit 4";
            $image_list=Db::query($s1);
            $image_count=count($image_list);
            if($image_count>0){
                foreach ($image_list as $k1=>$v1){
                    if($k1<4){
                        $localfile=$temp_path."image".$k1.".jpg";
                        ossdown($v1['oss_key'], $localfile,'timo-public');
                        suofang($localfile);
                    }
                }
            }
            else{
                $sql="update  content set is_video=3 where a_id=".$a_id;
                Db::execute($sql);
                $sql="update  ts_info_en set is_video=3 where a_id=".$a_id;
                Db::execute($sql);
                echo $a_id."-没有图片视频生成失败";
                echo "<br>";
            }
            
            $s2 = "select * from file_list where a_id='".$a_id."' AND type=2 AND language='eng' limit 1";
            $audio_en=Db::query($s2);
            $audioc_en_count=count($audio_en);
            if($audioc_en_count>0){
                $audiofile_en=$temp_path."audio-en.mp3";
                ossdown($audio_en[0]['oss_key'], $audiofile_en,'timo-video');
            }
            else{
                $sql="update  content set is_video=5 where a_id=".$a_id;
                Db::execute($sql);
                $sql="update  ts_info_en set is_video=5 where a_id=".$a_id;
                Db::execute($sql);
                echo $a_id."-没有英文音频视频生成失败";
                echo "<br>";
            }
            
            $s3 = "select * from file_list where a_id='".$a_id."' AND type=2 AND language='zh' limit 1";
            $audio_cn=Db::query($s3);
            $audioc_cn_count=count($audio_cn);
            if($audioc_cn_count>0){
                $audiofile_cn=$temp_path."audio-cn.mp3";
                ossdown($audio_cn[0]['oss_key'], $audiofile_cn,'timo-video');
            }
            else{
                $sql="update  content set is_video=4 where a_id=".$a_id;
                Db::execute($sql);
                $sql="update  ts_info_en set is_video=4 where a_id=".$a_id;
                Db::execute($sql);
                echo $a_id."-没有中文音频视频生成失败";
                echo "<br>";
            }
            $config_status_en=creat_video_config_file($temp_path,'en' ,$image_count);
          
            $config_status_cn=creat_video_config_file($temp_path,'cn' ,$image_count);
            if($config_status_en&&$config_status_cn){
                $config_file_cn=$temp_path."config-cn.txt";
                $video_cn=$temp_path."video-cn.mp4";
                $config_file_en=$temp_path."config-en.txt";
                $video_en=$temp_path."video-en.mp4";
                $s1=creat_mp4_re($config_file_cn,$audiofile_cn,$temp_path,$type,"zh");
                
                $s2=creat_mp4_re($config_file_en,$audiofile_en,$temp_path,$type,"eng");
          
                if($s1 && $s2 ){
                    $oss_cn_url=oss_upload_file($temp_path,$type,"video-cn.mp4",false,'timo-video');
                    $oss_en_url=oss_upload_file($temp_path,$type,"video-en.mp4",false,'timo-video');
                    $sql="insert into  file_list set bucket='timo-video', oss_key='".$oss_cn_url."', a_id=".$a_id.",type = 3,language = 'zh' ";
                    Db::execute($sql);
                    $sql="insert into  file_list set bucket='timo-video', oss_key='".$oss_en_url."', a_id=".$a_id.",type = 3,language = 'eng' ";
                    Db::execute($sql);
                    $sql="update  content set is_video=1 where a_id=".$a_id;
                    Db::execute($sql);
                    $sql="update  ts_info_en set is_video=1 where a_id=".$a_id;
                    Db::execute($sql);
                    $cmd ="rm -rf ".$temp_path;
                    exec($cmd);
                }
                else{
                    $sql="update  content set is_video=2 where a_id=".$a_id;
                    Db::execute($sql);
                    $sql="update  ts_info_en set is_video=2 where a_id=".$a_id;
                    Db::execute($sql);
                    echo $a_id."-视频生成失败";
                    echo "<br>";
                }
            }
            else{
                $sql="update  content set is_video=6 where a_id=".$a_id;
                Db::execute($sql);
                $sql="update  ts_info_en set is_video=6 where a_id=".$a_id;
                Db::execute($sql);
                echo $a_id."-音频异常导致配置文件出错视频生成失败";
                echo "<br>";
            }
        }
    }
    
    
    
    function tts(){
        tts_task('zh',5000);
    }
    
    
    function ttsen(){
        echo "开始时间:".date("Y-m-d h:i:s");
        echo "<br>";
        tts_task('eng',3800);
        echo "结束时间:".date("Y-m-d h:i:s");
        echo "<br>";
    }
   
   function testdown(){
      $s= catch_img('美女');
      var_dump($s);
      exit;
       
   }
    

    
    
    function tovideo(){
        echo date("Y-m-d h:i:s");
        echo "<br>";
        $sql ="select * from content where is_video < 1 limit 450";
        $result = Db::query($sql);
        $localpath="/var/www/timo/data/temp_video/";
        foreach ($result as $k=>$v){

            $a_id=$v['a_id'];
            $type=$v['type'];
            $s1 = "select * from file_list where a_id='".$a_id."'";
            $res=Db::query($s1);
            $x=0;
            $y=0;
            $z=0;
            $temp_path=$localpath.$a_id."/";
            if (!file_exists($temp_path)) {
                mkdir($temp_path, 0777);
                @chmod($temp_path, 0777);
            }
            foreach ($res as $k1=>$v1){
                
                if($v1['type']==1){
                    
                    if($x<4){
                       
                        
                        $localfile=$temp_path."image".$x.".jpg";
                        ossdown($v1['oss_key'], $localfile,'timo-public'); 
                        suofang($localfile);
                        $x++;
                    }
                    
                }
                
                if ($v1['type']==2){
                    if($v1['language']=='eng'){
                        if($y<1){

                            $audiofile_en=$temp_path."audio-en.mp3";
                            ossdown($v1['oss_key'], $audiofile_en,'timo-video');


                            $y++;
                        }
                    }
                    else{
                        if($z<1){
                            $audiofile_cn=$temp_path."/audio-cn.mp3";
                            ossdown($v1['oss_key'], $audiofile_cn,'timo-video');

                            $z++;
                        }
                    }
                }
            }
        if($x>0){
            
            if($y>0){
                $s3=creat_video_config_file($temp_path,'en' ,$x);
            }
            
            if($z>0){
                $s4=creat_video_config_file($temp_path,'cn' ,$x);
            }
            
            if($y>0&&$z>0){
                if($s3&&$s4){
                    $config_file_cn=$temp_path."config-cn.txt";
                    $video_cn=$temp_path."video-cn.mp4";
                    $config_file_en=$temp_path."config-en.txt";
                    $video_en=$temp_path."video-en.mp4";
                    $s1=creat_mp4($config_file_cn,$audiofile_cn,$temp_path,$type,"zh");
                    
                    $s2=creat_mp4($config_file_en,$audiofile_en,$temp_path,$type,"eng");
                    if($s1 && $s2 ){
                        $oss_cn_url=oss_upload_file($temp_path,$type,"video-cn.mp4",false,'timo-video');
                        $oss_en_url=oss_upload_file($temp_path,$type,"video-en.mp4",false,'timo-video');
                        $sql="insert into  file_list set bucket='timo-video', oss_key='".$oss_cn_url."', a_id=".$a_id.",type = 3,language = 'zh' ";
                        Db::execute($sql);
                        $sql="insert into  file_list set bucket='timo-video', oss_key='".$oss_en_url."', a_id=".$a_id.",type = 3,language = 'eng' ";
                        Db::execute($sql);
                        $sql="update  content set is_video=1 where a_id=".$a_id;
                        Db::execute($sql);
                        $sql="update  ts_info_en set is_video=1 where a_id=".$a_id;
                        Db::execute($sql);
                        $cmd ="rm -rf ".$temp_path;
                        exec($cmd);
                    }
                    else{
                        $sql="update  content set is_video=2 where a_id=".$a_id;
                        Db::execute($sql);
                        $sql="update  ts_info_en set is_video=2 where a_id=".$a_id;
                        Db::execute($sql);
                        echo $a_id."-视频生成失败";
                        echo "<br>";
                    }
                }
                else{
                    $sql="update  content set is_video=2 where a_id=".$a_id;
                    Db::execute($sql);
                    $sql="update  ts_info_en set is_video=2 where a_id=".$a_id;
                    Db::execute($sql);
                    echo $a_id."-音频异常导致配置文件出错视频生成失败";
                    echo "<br>";
                }
            }
           
            
           
           
            
        }
        else{
            $sql="update  content set is_video=2 where a_id=".$a_id;
            Db::execute($sql);
            $sql="update  ts_info_en set is_video=2 where a_id=".$a_id;
            Db::execute($sql);
            echo $a_id."-没有图片或者没有音频视频生成失败";
            echo "<br>";
        }
               
                
      }
        
        
        echo date("Y-m-d h:i:s")."-scuess";
        
    }
    
    function edit(){
        $a_id=$this->request->get('a_id');
        $where['a_id']=$a_id;
        
        $content= Db::name('content')->where($where)->find();
        
        $en_content= Db::name('ts_info_en')->where($where)->find();
        
        $cat_where=array("p_id"=>0);
        $cat_list=Db::name('content_type')->where($cat_where)->select();
        
        $sub_cat_where=array("p_id"=>$content['type']);
        $sub_cat= Db::name('content_type')->where($sub_cat_where)->select();
        
        $this->assign('cat_list', $cat_list);
        $this->assign('sub_cat', $sub_cat);
        
        $image_where=array('a_id'=>$a_id,'type'=>1);
        $audio_where=array('a_id'=>$a_id,'type'=>2);
        $video_where=array('a_id'=>$a_id,'type'=>3);
        $audio_title_where=array('a_id'=>$a_id,'type'=>4);
        
        $image_list= Db::name('file_list')->where($image_where)->limit(4)->select();
        $audio= Db::name('file_list')->where($audio_where)->select();
        $audio_title= Db::name('file_list')->where($audio_title_where)->select();
        $video= Db::name('file_list')->where($video_where)->select();
        
        
        $audio_count= count($audio);
        $audio_title_count= count($audio_title);
        $video_count= count($video);
        
        if($audio_count>0){
            foreach ($audio as $k=>$v){
                if($v['language']=='zh'){
                    $audio_cn=$v;
                }
                if($v['language']=='eng'){
                    $audio_en=$v;
                }
            }
            $this->assign('audio_cn', $audio_cn);
            $this->assign('audio_en', $audio_en);
        }
        
        if($video_count>0){
            foreach ($video as $k=>$v){
                if($v['language']=='zh'){
                    $video_cn=$v;
                }
                if($v['language']=='eng'){
                    $video_en=$v;
                }
            }
            $this->assign('video_cn', $video_cn);
            $this->assign('video_en', $video_en);
        }
        
        if($audio_title_count>0){
            foreach ($audio_title as $k=>$v){
                if($v['language']=='zh'){
                    $audio_title_cn=$v;
                }
                if($v['language']=='eng'){
                    $audio_title_en=$v;
                }
            }
            $this->assign('audio_title_cn', $audio_title_cn);
            $this->assign('audio_title_en', $audio_title_en);
        }
        
        
        $this->assign('content', $content);
        $this->assign('en_content', $en_content);
        $this->assign('image_list', $image_list);
    
        
        $this->assign('image_count', count($image_list));
        $this->assign('audio_count', $audio_count);
        $this->assign('video_count', $video_count);
        $this->assign('audio_title_count', $audio_title_count);
        
        
        return $this -> fetch();
    }
    
    
    function add(){
        $cat_where=array("p_id"=>0);
        $cat_list=Db::name('content_type')->where($cat_where)->select();
        $this->assign('cat_list', $cat_list);
        return $this -> fetch();
    }
    
    function index(){
        
        $language=$this->request->get('language');
        $page=$this->request->get('page');
        $name=$this->request->get('name');
        $type=$this->request->get('type');
        $type2=$this->request->get('type2');
      
        
        $where=array();
        $page_where=array();
        
        if(isset($type) &&$type!=''){
            $where['type']=$type;
            $page_where['type']=$type;
            $sub_cat_where=array("p_id"=>$type);
            $sub_cat= Db::name('content_type')->where($sub_cat_where)->select();
        }
        
        if(isset($type2) &&$type2!=''){
            $where['type2']=$type2;
            $page_where['type2']=$type2;
            
         
        }

        
        if(isset($name) && $name!=''){
            $where['name']=['like','%'.$name.'%'];
            $page_where['name']=$name;
        }
        
        
        $cat_where=array("p_id"=>0);
        $cat_list=Db::name('content_type')->where($cat_where)->select();
        
        //var_dump($cat_list);exit;
        $content= Db::name('content')->where($where)->order('a_id', 'desc')->paginate(15,false,['query' => $page_where]);
        $page = $content->render();
        $data = $content->all();

        foreach ($data as $k=>$v){
            $data[$k]['type_cn']="";
            foreach ($cat_list as $k1=>$v1){
                if($v["type"]==$v1['type_id']){
                    $data[$k]['type_cn']=$v1['type_name'];
                }
                
            }
           
        }
        
        if(isset($sub_cat)&& count($sub_cat)>0){
            $is_sub_cat=1;
        }
        else{
            $is_sub_cat=0;
            $sub_cat=array();
        }
            
        
        $this->assign('cat_list', $cat_list);
        $this->assign('type', $type);
        $this->assign('name', $name);
        $this->assign('list', $data);
        $this->assign('page', $page);
        $this->assign('sub_cat', $sub_cat);
        $this->assign('type2', $type2);
        $this->assign('is_sub_cat', $is_sub_cat);
    
        return $this -> fetch();
    }
    
    function del(){
        $a_id=$this->request->get('a_id');
    }
    
    
}
